#include "equipmentshop.h"
#include "ui_equipmentshop.h"
#include<QWidget>
#include<QLabel>
#include<QKeyEvent>
#include<QPixmap>
#include<QPushButton>
#include<qrect.h>
#include<QFont>
#include"menu.h"
#include <QDebug>
#include<QTime>
#include<ctime>
#include"mainwindow.h"
Equipmentshop::Equipmentshop()
{
    click=0;
    saltstore=2;
    money=50;
    shengmustore=2;
    takeawayshengmu=0;
    salt=0;
    //生成各种按钮的界面
    takeawayshengmu=0;
    this->setGeometry(435,100,700,700);
    startRealgameButton=new QPushButton("前往鬼房",this);
    startRealgameButton->move(590,580);
    startRealgameButton->resize(100,50);
    //关于圣木的按钮
    buyshengmu=new QPushButton("+",this);
    buyshengmu->move(380,210);
    buyshengmu->resize(31,31);
    B_increasetakeawayshengmu=new QPushButton("+",this);
    B_increasetakeawayshengmu->move(380,250);
    B_increasetakeawayshengmu->resize(31,31);
    B_reducetakeawayshengmu=new QPushButton("-",this);
    B_reducetakeawayshengmu->move(420,250);
    B_reducetakeawayshengmu->resize(31,31);
    //关于盐的按钮
    buysalt=new QPushButton("+",this);
    buysalt->move(380,360);
    buysalt->resize(31,31);
    increasesalt=new QPushButton("+",this);
    increasesalt->move(380,400);
    increasesalt->resize(31,31);
    reducesalt=new QPushButton("-",this);
    reducesalt->move(420,400);
    reducesalt->resize(31,31);
    //难度调节按钮
    difficultydegree=new QPushButton("普通",this);
    difficultydegree->move(100,580);
    difficultydegree->resize(100,50);
    timer1=new QTimer(this);//设定计时器
    timer1->start(100);
    setFixedSize(700,700);
    setWindowTitle("装备车");
    connect(timer1,SIGNAL(timeout()),this,SLOT(Equipmentshop_update()));
    connect(B_reducetakeawayshengmu,&QPushButton::clicked,this,&Equipmentshop::reducetakeawayshengmu);//连接减少带走圣母
    connect(B_increasetakeawayshengmu,&QPushButton::clicked,this,&Equipmentshop::increasetakeawayshengmu);//连接增加带走圣母
    connect(buyshengmu,&QPushButton::clicked,this,&Equipmentshop::increaseshengmu);//连接增加仓库圣木
    connect(startRealgameButton,&QPushButton::clicked,this,&Equipmentshop::startrealgame);//连接开始游戏
    connect(reducesalt,&QPushButton::clicked,this,&Equipmentshop::reducenumbersalttaken);
    connect(increasesalt,&QPushButton::clicked,this,&Equipmentshop::increasenumbersalttaken);
    connect(buysalt,&QPushButton::clicked,this,&Equipmentshop::increase_salt);
    connect(difficultydegree,&QPushButton::clicked,this,&Equipmentshop::degreechange);
}
Equipmentshop::Equipmentshop(int moneynumber,int shengmustore1)
{
    //生成各种按钮的界面
    takeawayshengmu=0;
    salt=0;
    click=0;
    this->setGeometry(435,100,700,700);
    startRealgameButton=new QPushButton("前往鬼房",this);
    startRealgameButton->move(590,580);
    startRealgameButton->resize(100,50);
    //关于圣木的按钮
    buyshengmu=new QPushButton("+",this);
    buyshengmu->move(380,210);
    buyshengmu->resize(31,31);
    B_increasetakeawayshengmu=new QPushButton("+",this);
    B_increasetakeawayshengmu->move(380,240);
    B_increasetakeawayshengmu->resize(31,31);
    B_reducetakeawayshengmu=new QPushButton("-",this);
    B_reducetakeawayshengmu->move(420,250);
    B_reducetakeawayshengmu->resize(31,31);
    //关于盐的按钮
    buysalt=new QPushButton("+",this);
    buysalt->move(380,360);
    buysalt->resize(31,31);
    increasesalt=new QPushButton("+",this);
    increasesalt->move(380,400);
    increasesalt->resize(31,31);
    reducesalt=new QPushButton("-",this);
    reducesalt->move(420,390);
    reducesalt->resize(31,31);
    //难度调节按钮
    difficultydegree=new QPushButton("普通",this);
    difficultydegree->move(100,580);
    difficultydegree->resize(100,50);
    timer1=new QTimer(this);//设定计时器
    timer1->start(100);
    setFixedSize(700,700);
    setWindowTitle("装备车");
    connect(timer1,SIGNAL(timeout()),this,SLOT(Equipmentshop_update()));
    connect(B_reducetakeawayshengmu,&QPushButton::clicked,this,&Equipmentshop::reducetakeawayshengmu);//连接减少带走圣母
    connect(B_increasetakeawayshengmu,&QPushButton::clicked,this,&Equipmentshop::increasetakeawayshengmu);//连接增加带走圣母
    connect(buyshengmu,&QPushButton::clicked,this,&Equipmentshop::increaseshengmu);//连接增加仓库圣木
    connect(startRealgameButton,&QPushButton::clicked,this,&Equipmentshop::startrealgame);//连接开始游戏
    connect(reducesalt,&QPushButton::clicked,this,&Equipmentshop::reducenumbersalttaken);
    connect(increasesalt,&QPushButton::clicked,this,&Equipmentshop::increasenumbersalttaken);
    connect(buysalt,&QPushButton::clicked,this,&Equipmentshop::increase_salt);
    connect(difficultydegree,&QPushButton::clicked,this,&Equipmentshop::degreechange);
    money=moneynumber;
    shengmustore=shengmustore1;
    saltstore=2;
}
//Equipmentshop::~Equipmentshop()
//{
//    delete ui;
//}
//void Equipmentshop::paintEvent(QPaintEvent*event)
//{

//}
void Equipmentshop::keyPressEvent(QKeyEvent *event)
{
    QKeyEvent *key=(QKeyEvent*)event;
    switch(key->key())
    {
    case Qt::Key_Escape:
        //emit sendsignal();
        this->hide();
        menu1->show();
        break;
    }
}
void Equipmentshop::startrealgame()
{
    this->hide();
    MainWindow *ui_mainwindow=new MainWindow(money,takeawayshengmu,shengmustore,salt,click);
    ui_mainwindow->equipmentshop1=this;
    ui_mainwindow->show();
}
void Equipmentshop::paintEvent(QPaintEvent*event)
{
    QPainter painter(this);
    QFont font1("Micro soft YaHei UZ",15);
    painter.setFont(font1);
    painter.setPen(Qt::black);
    painter.setBrush(Qt::black);
    painter.drawText(290,180,"圣木");
    painter.drawText(290,220,"库存");
    painter.drawText(290,260,"装备");
    painter.drawText(340,180,"10RMB");
    painter.drawText(340,220,QString::number(shengmustore));
    painter.drawText(340,260,QString::number(takeawayshengmu));
    painter.drawText(100,30,QString::number(money));
    painter.drawText(10,30,"金币数量");
    painter.drawText(290,330,"盐");
    painter.drawText(290,370,"库存");
    painter.drawText(290,410,"装备");
    painter.drawText(340,330,"15RMB");
    painter.drawText(10,610,"难度选择:");
    painter.drawText(340,370,QString::number(saltstore));
    painter.drawText(340,410,QString::number(salt));
    painter.drawPixmap(200,190,70,70,QPixmap(":/images/myImages/shengmu.png"));
    painter.drawPixmap(200,325,70,70,QPixmap(":/images/myImages/salt.png"));
}
void Equipmentshop::increaseshengmu()
{
    if(money<10)
        return;
    else
     {
        money=money-10;
        shengmustore=shengmustore+1;
     }
}
void Equipmentshop::increasetakeawayshengmu()
{
    if(takeawayshengmu+1>6||takeawayshengmu+1>shengmustore)
        return;
    else
    {
        takeawayshengmu++;
    }
}
void Equipmentshop::reducetakeawayshengmu()
{
    if(takeawayshengmu-1<0)
        return;
    else
    {
        takeawayshengmu--;
    }
}

void Equipmentshop::increase_salt()
{
    if(money<15)
        return;
    else
    {
        money=money-15;
        saltstore=saltstore+1;
    }
}
void Equipmentshop::reducenumbersalttaken()
{
    if(salt>0)
    {
        salt =salt-1;
    }
}
void Equipmentshop::increasenumbersalttaken()
{
    if(salt<4&&salt<saltstore)
    {
        salt=salt+1;
    }
}
void Equipmentshop::Equipmentshop_update()
{
    update();
}
void Equipmentshop::degreechange()
{
    click++;
    if(click%2==0)
    {
        difficultydegree->setText("普通");
    }
    else
    {
        difficultydegree->setText("专家");
    }
}
